
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../data/banner/banner_entity.dart';
import 'carousel_image.dart';
import 'circular_indicator.dart';
import 'home_view_model.dart';

/// 业务组件 - 首页banner
class HomeBanner extends StatefulWidget {
  const HomeBanner({super.key});

  @override
  State<HomeBanner> createState() => _HomeBannerState();
}

class _HomeBannerState extends State<HomeBanner> {
  int _pageIndex = 0;
  @override
  void initState() {
    super.initState();
    context.read<HomeViewModel>().requestBannerList();
  }

  @override
  Widget build(BuildContext context) {
    final bannerList = context.select<HomeViewModel, List<BannerEntity>>(
          (viewModel) => viewModel.bannerList,
    );

    return Stack(
      alignment: Alignment.bottomCenter,
      children: [
        CarouselImage(list: bannerList,onPageChange: (index){
          setState(() {
            _pageIndex = index;
          });
        },),
        Positioned(
          bottom: 10,
          child: CircularIndicator(count: bannerList.length,index: _pageIndex,),
        )
      ],
    );
  }
}
